home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / update-notifier / apt-check < prev    next >
Encoding:
Text File  |  2007-04-10  |  3.2 KB  |  120 lines

  1. #!/usr/bin/python
  2.  
  3.  
  4. #nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst 
  5.  
  6. import apt_pkg
  7. import os
  8. import sys
  9. from optparse import OptionParser
  10. from gettext import gettext as _
  11. import gettext
  12. SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"
  13.  
  14. def clean(cache,depcache):
  15.     # mvo: looping is too inefficient with the new auto-mark code
  16.     #for pkg in cache.Packages:
  17.     #    depcache.MarkKeep(pkg)
  18.     depcache.Init()
  19.  
  20. def saveDistUpgrade(cache,depcache):
  21.     """ this functions mimics a upgrade but will never remove anything """
  22.     depcache.Upgrade(True)
  23.     if depcache.DelCount > 0:
  24.         clean(cache,depcache)
  25.     depcache.Upgrade()
  26.  
  27. def _handleException(type, value, tb):
  28.     sys.stderr.write("E: "+ "Unkown Error: '%s' (%s)" % (type,value))
  29.     sys.exit(-1)
  30.  
  31.  
  32. # -------------------- main ---------------------
  33.         
  34. # be nice
  35. os.nice(19)
  36.  
  37. # setup a exception handler to make sure that uncaught stuff goes
  38. # to the notifier
  39. sys.excepthook = _handleException
  40.  
  41. # gettext
  42. APP="update-notifier"
  43. DIR="/usr/share/locale"
  44. gettext.bindtextdomain(APP, DIR)
  45. gettext.textdomain(APP)
  46.  
  47. # check arguments
  48. parser = OptionParser()
  49. parser.add_option("-s",
  50.                   "--security-only",
  51.                   action="store_true",
  52.                   dest="security_only",
  53.                   help="show only security updates")
  54. parser.add_option("-p",
  55.                   "--package-names",
  56.                   action="store_true",
  57.                   dest="show_package_names",
  58.                   help="show the packages that are going to be installed/upgraded")
  59. (options, args) = parser.parse_args()
  60. #print options.security_only
  61.  
  62. # init
  63. apt_pkg.init()
  64.  
  65. # get caches
  66. try:
  67.     cache = apt_pkg.GetCache()
  68. except SystemError, e:
  69.     sys.stderr.write("E: "+ _("Error: Opening the cache (%s)") % e)
  70.     sys.exit(-1)
  71. depcache = apt_pkg.GetDepCache(cache)
  72.  
  73. # read the pin files
  74. depcache.ReadPinFile()
  75. # read the synaptic pins too
  76. if os.path.exists(SYNAPTIC_PINFILE):
  77.     depcache.ReadPinFile(SYNAPTIC_PINFILE)
  78.  
  79. # init the depcache
  80. depcache.Init()
  81.  
  82. if depcache.BrokenCount > 0:
  83.     sys.stderr.write("E: "+ _("Error: BrokenCount > 0"))
  84.     sys.exit(-1)
  85.  
  86. # do the upgrade (not dist-upgrade!)
  87. try:
  88.     saveDistUpgrade(cache,depcache)
  89. except SystemError, e:
  90.     sys.stderr.write("E: "+ _("Error: Marking the upgrade (%s)") % e)
  91.     sys.exit(-1)
  92.  
  93. # check for upgrade packages, we need to do it this way
  94. # because of ubuntu #7907
  95. upgrades = 0
  96. security_updates = 0
  97. for pkg in cache.Packages:
  98.     if depcache.MarkedInstall(pkg) or depcache.MarkedUpgrade(pkg):
  99.         # check if this is really a upgrade or a false positive
  100.         # (workaround for ubuntu #7907)
  101.     if depcache.GetCandidateVer(pkg) != pkg.CurrentVer:
  102.         upgrades = upgrades + 1    
  103.                 ver = depcache.GetCandidateVer(pkg)
  104.                 for (file, index) in ver.FileList:
  105.                     if file.Archive == "hoary-security":
  106.                         security_updates += 1
  107.  
  108. # print the number of upgrades
  109. if options.show_package_names:
  110.     pkgs = filter(lambda pkg: depcache.MarkedInstall(pkg) or depcache.MarkedUpgrade(pkg), cache.Packages)
  111.     sys.stderr.write("\n".join(map(lambda p: p.Name, pkgs)))
  112. elif options.security_only:
  113.     sys.stderr.write("%s" % security_updates)
  114. else:
  115.     sys.stderr.write("%s" % upgrades)
  116.  
  117.  
  118.  
  119. sys.exit(0)
  120.